package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.wfg.dialog.startactivitydialog.SaturnStartActivityDialog;
import edu.ccut.saturn.studio.wfg.figure.SaturnFigure;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class SaturnStartNode extends Shape {

	private static final long serialVersionUID = 1L;
	
	private SaturnStartLimit saturnStartLimit = new SaturnStartLimit();
	
	private String startPolicy = "1";
	
	private String timeLimit = "";
	
	private String offsetMode = "1";
	
	private List<SaturnAttribute> logicKeyList = new ArrayList<SaturnAttribute>();

	private List<SaturnAttribute> formDataAttributeList = new ArrayList<SaturnAttribute>();
	
	private Image startPictureSmall = createImage(Activator.SMALL_START_ICON);
	
	public SaturnStartNode() {
		super.setName(Messages.getString("SaturnStartNode.start"));   //$NON-NLS-1$
		super.setSize(new Dimension(50, 50));
	}
	
	public Image getIcon() {
		return Activator.getImage(Images.BIG_START_ICON);
	}
	
	public Image getSmallIcon() {
		return startPictureSmall;
	}

	public void setSmallIcon(Image startPictureSmall) {
		this.startPictureSmall = startPictureSmall;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(Activator.getImage(Images.BIG_START_ICON));
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}
	
	public String getType() {
		return SaturnName.KEY_NODE_ATTRIBUTE_TYPENAMESTART;
	}
	
	public Dialog getDialog() {
		SaturnStartActivityDialog dialog = new SaturnStartActivityDialog(Display.getDefault().getShells()[0], this);
		return dialog;
	}
	
	@Override
	protected Node createChildNode(Document document, SaturnAttribute attrib, String name) {
		return attrib.creatNode(document, name);
	}
	
	@Override
	public Node doRefresh(Document document) {
		Node startHumanNode = document.createElement(SaturnName.XML_NAME);
		Node rootNode = document.getDocumentElement();
		// 基本信息
		SaturnW3CUtil.createAttribute(startHumanNode, SaturnName.XML_TYPE, getType());
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_TIMELIMIT, this.timeLimit);
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_STARTPOLICY, this.startPolicy);
		SaturnW3CUtil.createChildNode(startHumanNode, SaturnName.KEY_NODE_ELEMENT_OFFSETMODE, this.offsetMode);
		// logicKey
		super.createNodes(document, startHumanNode, this.getLogicKeyList(), SaturnName.KEY_NODE_ELEMENT_LOGICKEY, SaturnName.KEY_NODE_NAME_KEY);
		// formData
		super.createNodes(document, startHumanNode, this.getFormDataAttributeList(),SaturnName.KEY_NODE_ELEMENT_FORMDATA,SaturnName.KEY_NODE_NAME_DATA);
		// 启动限制
		startHumanNode.appendChild(saturnStartLimit.createNode(document, SaturnName.XML_NAME));
		rootNode.appendChild(startHumanNode);
		return startHumanNode;
	}
	
	
	@Override
	public Shape cloneShape(Shape shape) {
		SaturnStartNode saturnStartNode = (SaturnStartNode)shape;
		SaturnStartNode newSaturnStartNode = new SaturnStartNode();
		newSaturnStartNode.setName(saturnStartNode.getName());
		newSaturnStartNode.setId(saturnStartNode.getId());
		newSaturnStartNode.setNextId(saturnStartNode.getNextId());
		newSaturnStartNode.setDescription(saturnStartNode.getDescription());
		newSaturnStartNode.setStartPolicy(saturnStartNode.getStartPolicy());
		newSaturnStartNode.setTimeLimit(saturnStartNode.getTimeLimit());
		newSaturnStartNode.setOffsetMode(saturnStartNode.getOffsetMode());
		newSaturnStartNode.setSmallIcon(saturnStartNode.getSmallIcon());
		return newSaturnStartNode;
	}

	public SaturnStartLimit getSaturnStartLimit() {
		return saturnStartLimit;
	}

	public void setSaturnStartLimit(SaturnStartLimit saturnStartLimit) {
		this.saturnStartLimit = saturnStartLimit;
	}

	public String getStartPolicy() {
		return startPolicy;
	}

	public void setStartPolicy(String startPolicy) {
		this.startPolicy = startPolicy;
	}

	public String getTimeLimit() {
		return timeLimit;
	}

	public void setTimeLimit(String timeLimit) {
		this.timeLimit = timeLimit;
	}

	public String getOffsetMode() {
		return offsetMode;
	}

	public void setOffsetMode(String offsetMode) {
		this.offsetMode = offsetMode;
	}

	public List<SaturnAttribute> getLogicKeyList() {
		return logicKeyList;
	}

	public void setLogicKeyList(List<SaturnAttribute> logicKeyList) {
		this.logicKeyList = logicKeyList;
	}

	public List<SaturnAttribute> getFormDataAttributeList() {
		return formDataAttributeList;
	}

	public void setFormDataAttributeList(List<SaturnAttribute> formDataAttributeList) {
		this.formDataAttributeList = formDataAttributeList;
	}
	
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace("logic" , observer, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "logic",
					observer, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
}

	@Override
	public Parameters createNodeHtml(String filePath) {
		
		Map<String, String> infos = new HashMap<String, String>();
		Parameters para = new Parameters();
		String name = getName();

		infos.put("name", getName());
		infos.put("id", getId());
		infos.put("startpolicy", transferstart(getStartPolicy()));
		infos.put("timelimit", getTimeLimit());
		infos.put("offsetmode", Messages.getString("Wfg.OneSwitch")); //$NON-NLS-2$
		infos.put("description", getDescription());

		para.setInfourl(name + ".html");
		para.setInfoname(name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Wfg.StartNode")); //$NON-NLS-1$
		para.setImagename("start");
		String templatePath = "template/export/wfg/start";
		String Path = filePath + File.separator + name + ".html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);
		
		return para;
	}
	
	/**
	 * 对启动策略进行转换
	 * 
	 * @param startPolicy
	 * @return Parameters
	 */
	private static String transferstart(String startPolicy) {
		
		if (startPolicy.equals("1")) {
			return Messages.getString("Wfg.HumanStart"); //$NON-NLS-1$
		} else {
			return Messages.getString("Wfg.ActionStart"); //$NON-NLS-1$
		}
	}
}
